﻿using BP.CCFast.Portal;
using BP.CCFast.Portal.WindowExt;
using BP.DA;
using BP.Difference;
using BP.En;
using BP.Port;
using BP.Sys;
using BP.Web;
using BP.WF;
using BP.WF.Admin;
using BP.WF.Data;
using BP.WF.HttpHandler;
using BP.WF.Rpt;
using BP.WF.Template;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;

namespace BP.CCFast
{
    /// <summary>
    /// 页面功能实体
    /// </summary>
    public class DataV_Lowcode : DirectoryPageBase
    {
        /// <summary>
        /// 页面功能实体
        /// </summary>
        public DataV_Lowcode()
        {
        }
        public string Init_LowcodePage()
        {
            DataSet ds = new DataSet(); //定义容器.

            string pageID = this.GetRequestVal("PageID");

            WindowTemplates windows = new WindowTemplates();
            windows.Retrieve(WindowTemplateAttr.PageID, pageID, "Idx");

            foreach (WindowTemplate window in windows)
            {
                if (window.WinDocModel.Equals(WinDocModel.Html))
                    continue;
                #region SSO
                if (window.WinDocModel.Equals(WinDocModel.SSO) == true)
                {
                    SSODtls dtls = new SSODtls(); //获得明细.
                    dtls.Retrieve("RefPK", window.No, "Idx");
                    //加入集合.
                    DataTable mydt = dtls.ToDataTableField("SSODtls" + window.No);
                    ds.Tables.Add(mydt);
                }
                #endregion SSO
                #region 变量文本
                if (window.WinDocModel.Equals(WinDocModel.HtmlVar) == true)
                {
                    HtmlVarDtls dtls = new HtmlVarDtls(); //获得明细.
                    dtls.Retrieve("RefPK", window.No, "Idx");
                    foreach (HtmlVarDtl dtl in dtls)
                    {
                        if (dtl.Exp0.ToLower().Contains("select") == false)
                            continue;
                        if (DataType.IsNullOrEmpty(window.WinDocModel) == true)
                            continue;
                        string exp0 = BP.WF.Glo.DealExp(dtl.Exp0, null);
                        try
                        {
                            DataTable dtt = SFDBSrcs.RunSQLReturnTable(exp0, dtl.DBSrc);
                            if (dtt == null || dtt.Rows.Count == 0)
                                dtl.Exp0 = "0";
                            string val = dtt.Rows[0][0].ToString();
                            dtl.Exp0 = val;

                        }
                        catch(Exception ex)
                        {
                            dtl.Exp0 = "err@" + ex.Message;
                        }
                        
                    }

                    //加入集合.
                    DataTable mydt = dtls.ToDataTableField("HtmlVarDtls" + window.No);
                    ds.Tables.Add(mydt);
                }
                #endregion 变量文本

                #region 折线图
                if (window.WinDocModel.Equals(WinDocModel.ChartLine) == true
                       || window.WinDocModel.Equals(WinDocModel.ChartRing) == true
                       || window.WinDocModel.Equals(WinDocModel.ChartPie) == true
                       || window.WinDocModel.Equals(WinDocModel.ChartLine) == true
                       || window.WinDocModel.Equals(WinDocModel.ChartZZT) == true
                    || window.WinDocModel.Equals(WinDocModel.Table) == true || window.WinDocModel.Equals(WinDocModel.ChartRose) == true || window.WinDocModel.Equals(WinDocModel.ChartLineAdv) == true || window.WinDocModel.Equals(WinDocModel.ChartRadar) == true)
                {

                    if (DataType.IsNullOrEmpty(window.WinDocModel) == true)
                        continue;

                    try
                    {
                        string sql = window.Docs;
                        sql = BP.WF.Glo.DealSQLExp(sql, null, null);
                        window.Docs = SFDBSrcs.RunSQLReturnTableJson(sql, window.DBSrc);
                    }
                    catch (Exception ex)
                    {
                        window.Docs = "err@:"+ex.Message;
                    }
                    try
                    {
                        string C0Ens = window.GetValStrByKey("C0Ens");
                        if (DataType.IsNullOrEmpty(C0Ens) == false)
                            window.SetValByKey("C0Ens", SFDBSrcs.RunSQLReturnTableJson(C0Ens, window.DBSrc));
                    }catch(Exception ex)
                    {
                        window.SetValByKey("C0Ens", "err@:" + ex.Message);
                    }
                   
                }
                #endregion 折线图
                if (window.WinDocModel.Equals(WinDocModel.ChartRate) == true)
                {
                    string SQLOfFZ = window.GetValStringByKey("SQLOfFZ");
                    string SQLOfFM = window.GetValStringByKey("SQLOfFM");
                    if (DataType.IsNullOrEmpty(SQLOfFZ) || DataType.IsNullOrEmpty(SQLOfFM))
                    {
                        continue;
                    }
                    try
                    {
                        int fzCount = DBAccess.RunSQLReturnValInt(SQLOfFZ.Replace("~", "'"));
                        int fmCount = DBAccess.RunSQLReturnValInt(SQLOfFM.Replace("~", "'"));
                        window.Docs = "[{\"Name\":\"" + window.GetValStringByKey("LabOfFZ") + "\",\"Num\":" + fzCount + "},{\"Name\":\"" + window.GetValStringByKey("LabOfFM") + "\",\"Num\":" + fmCount + "}]";
                    }catch(Exception ex)
                    {
                        window.Docs = "err@" + ex.Message;
                    }
                   
                }
            }

            //增加到json.
            DataTable dt = windows.ToDataTableField("Windows");
            ds.Tables.Add(dt);

            //json.
            return BP.Tools.Json.ToJson(ds);
        }

    }
}
